import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

//库函数已经写好了arraylist,可以直接用
public class Test {

    public static void main(String[] args) {

        ArrayList<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        //遍历数组
        //1最简单的for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
        //2for each循环，缺点，拿不到下标，只能遍历
        //:左边是集合中元素类型所定义的变量，右边是要遍历到集合
        for (Integer x:list){
            System.out.print(x+" ");
        }
        //3迭代器
        //(1)
        Iterator<Integer>it= list.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        //(2)
        ListIterator<Integer> listIterator= list.listIterator();
        while (listIterator.hasNext()){
            System.out.print(listIterator.next());
        }
        //倒序打印
        ListIterator<Integer> listIterator2= list.listIterator(list.size());
        while (listIterator2.hasPrevious()){
            System.out.print(listIterator2.previous());
        }
    }
}
